package com.ruoyi.crm.strategy.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.crm.strategy.ImageStorageStrategy;

/**
 * 本地存储策略实现
 * 
 * @author ruoyi
 * @date 2025-11-03
 */
@Component
public class LocalStorageStrategy implements ImageStorageStrategy {
    
    @Value("${ruoyi.profile}")
    private String profile;
    
    @Value("${ruoyi.upload.path}")
    private String uploadPath;

    @Override
    public String uploadImage(MultipartFile file) throws Exception {
        // 文件类型校验
        String[] allowedExtension = {"png", "jpg", "jpeg", "gif", "bmp"};
        String fileName = FileUploadUtils.upload(uploadPath, file, allowedExtension);
        // 返回相对路径，让前端通过/profile/**访问
        return Constants.RESOURCE_PREFIX + "/" + fileName;
    }

    @Override
    public void deleteImage(String imageUrl) throws Exception {
        // 本地存储删除逻辑
        // 这里可以实现具体的删除逻辑
    }

    @Override
    public String getStrategyName() {
        return "local";
    }
}